package com.hzy.pms.controller;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.hzy.pms.domain.vo.ProductCategoryVo;
import com.hzy.pms.domain.bo.ProductCategoryBo;
import com.hzy.pms.service.IProductCategoryService;

/**
 * 商品分类
 *
 * @author zengxin
 * @date 2023-10-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pms/productCategory")
public class ProductCategoryController extends BaseController {

    private final IProductCategoryService iProductCategoryService;

    /**
     * 查询商品分类列表
     */
    @SaCheckPermission("pms:productCategory:list")
    @GetMapping("/list")
    public R<List<ProductCategoryVo>> list(ProductCategoryBo bo) {
        List<ProductCategoryVo> list = iProductCategoryService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出商品分类列表
     */
    @SaCheckPermission("pms:productCategory:export")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ProductCategoryBo bo, HttpServletResponse response) {
        List<ProductCategoryVo> list = iProductCategoryService.queryList(bo);
        ExcelUtil.exportExcel(list, "商品分类", ProductCategoryVo.class, response);
    }

    /**
     * 获取商品分类详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("pms:productCategory:query")
    @GetMapping("/{id}")
    public R<ProductCategoryVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iProductCategoryService.queryById(id));
    }

    /**
     * 新增商品分类
     */
    @SaCheckPermission("pms:productCategory:add")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ProductCategoryBo bo) {
        return toAjax(iProductCategoryService.insertByBo(bo));
    }

    /**
     * 修改商品分类
     */
    @SaCheckPermission("pms:productCategory:edit")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ProductCategoryBo bo) {
        return toAjax(iProductCategoryService.updateByBo(bo));
    }

    /**
     * 删除商品分类
     *
     * @param ids 主键串
     */
    @SaCheckPermission("pms:productCategory:remove")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iProductCategoryService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
